/**
 * 
 */
package org.ucn.dmaa0914.gruppe6;

/**
 * @author twicfall
 *
 */
public class ItemCtr extends CrudController<Item> {
	private static ItemCtr instance;
	private int currentItem;
	
	public static ItemCtr getInstance()
	{
		if( instance == null )
			instance = new ItemCtr();
		
		return instance;
	}

	@Override
	protected SearchFilter<Item> getSearchFilter() {
		return new ItemSearchFilter();
	}

	@Override
	protected Item newObject() {
		return new Item();
	}

	public void setCurrentItem(int selectedValue) {
		currentItem = selectedValue;
	}
	
	public int getCurrentItem()
	{
		return currentItem;
	}
	
	public void updateItem(int id, String barcode, String title, double price, String currency)
	{
		Item i = find(id);
		
		if( barcode != null )
			i.setBarcode(barcode);
		
		if( title != null )
			i.setTitle(title);
		
		if( price >= 0.0 )
			i.setPrice(price);
		
		if( currency != null )
			i.setCurrency(currency);
	}

	@Override
	public String format(int id) {
		return String.format("%s (%s)", find(id).getTitle(), find(id).getBarcode());
	}
}
